
using Microsoft.AspNetCore.HttpLogging;
using System.Text;

namespace Cngot.Extensions.DependencyInjection.WebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            Console.InputEncoding = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;

            builder.Configuration.AddJsonFile("Serilog.json", true, true);

            builder.Services.AddCngotLogging(options =>
            {
                builder.Configuration.GetSection("LoggerOption").Bind(options);
                options.AppId = builder.Configuration.GetValue<string>("Apollo:AppId");
                options.AllowedAssemblies = new List<string>
                {
                    "Cngot.Extensions.EventBus",
                    "Cngot.Extensions.EventBus.RabbitMQ",
                    "Cngot.Demo.WebApi",
                };
            }, (logger) =>
            {
                logger.UseFileAsync(q => builder.Configuration.GetSection("LoggerOption").Bind(q));
            });
            builder.Services.AddCngotHttpLogging(w =>
            {
                w.CombineLogs = true;
                w.LoggingFields = HttpLoggingFields.Duration | HttpLoggingFields.RequestBody | HttpLoggingFields.ResponseBody;
            });

            builder.Services.AddControllers();
            builder.Services.AddSingleton<ITestService, TestService>();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var app = builder.Build();


            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseCngotHttpLogging();

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}
